package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IFlowerFloridDao;
import com.hamizhong.iflowers.db.entity.FlowerFlorid;
import com.hamizhong.iflowers.service.IFlowerFloridService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.FlowerFloridVO;
import com.hamizhong.iflowers.web.vo.IObjectVO;

public class FlowerFloridServiceImpl extends MyBaseService implements IFlowerFloridService {
	private IFlowerFloridDao iFlowerFloridDao = null;

	public IFlowerFloridDao getiFlowerFloridDao() {
		return iFlowerFloridDao;
	}
	public void setiFlowerFloridDao(IFlowerFloridDao iFlowerFloridDao) {
		this.iFlowerFloridDao = iFlowerFloridDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<FlowerFlorid> flowerFloridList = new ArrayList<FlowerFlorid>();
		flowerFloridList = iFlowerFloridDao.findFlowerFlorid(pageNum, pageSize);
		int totalFlowerFloridCnt = iFlowerFloridDao.findTotalFlowerFloridCnt();
		List<FlowerFloridVO> flowerFloridVOList = new ArrayList<FlowerFloridVO>();
		FlowerFloridVO flowerFloridVO = null;
		for (FlowerFlorid flowerFlorid : flowerFloridList) {
			flowerFloridVO = new FlowerFloridVO();
			BeanUtils.copyProperties(flowerFlorid, flowerFloridVO);
			flowerFloridVOList.add(flowerFloridVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setFlowerFloridVOList(flowerFloridVOList);
		iObjectVO.setTotalFlowerFloridCnt(totalFlowerFloridCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addFlowerFlorid(FlowerFloridVO flowerFloridVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		FlowerFlorid flowerFlorid = new FlowerFlorid();
		try {
			BeanUtils.copyProperties(flowerFloridVO, flowerFlorid);
			iFlowerFloridDao.add(flowerFlorid);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updateFlowerFlorid(FlowerFloridVO flowerFloridVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		FlowerFlorid flowerFlorid = new FlowerFlorid();
		try {
			BeanUtils.copyProperties(flowerFloridVO, flowerFlorid);
			iFlowerFloridDao.update(flowerFlorid);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deleteFlowerFlorid(FlowerFloridVO flowerFloridVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			int flowerFloridId = flowerFloridVO.getFlowerFloridId();
			FlowerFlorid flowerFlorid = iFlowerFloridDao.findById(flowerFloridId);
			if(null != flowerFlorid) {
				this.iFlowerFloridDao.delete(flowerFlorid);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
	
}
